"use strict";
const { Model, Sequelize } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Contract extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      Contract.belongsTo(models.Project, {
        foreignKey: "project_id",
        allowNull: true,
      });
      // Contract.belongsTo(models.Client,{
      //   foreignKey:'client_id',
      //   // onDelete: 'RESTRICT',
      //   // OnUpdate: 'CASCADE',
      // })// define association here
      Contract.belongsTo(models.Staff, {
        foreignKey: "creator_id",
        as: "creator",
        allowNull: true,
        // onDelete: 'SET NULL',
        // onUpdate: 'CASCADE'
      });
      Contract.belongsTo(models.Staff, {
        foreignKey: "checker_id",
        as: "checker",
        allowNull: true,
      });
      Contract.hasMany(models.Shop, {
        foreignKey: "contract_id",
      });
      Contract.hasMany(models.ContractPayment, {
        foreignKey: "contract_id",
      });
      Contract.hasMany(models.Receipt, {
        foreignKey: "contract_id",
      });
      // Contract.hasMany(models.Service,{
      //   foreignKey:'contract_id'
      // })
      // Contract.hasMany(models.Product,{
      //   foreignKey:'product_id',
      //   onDelete: 'NO ACTION',
      //   onUpdate: 'CASCADE'
      // })
      // Contract.hasMany(models.Factory,{
      //   foreignKey:'factory_id',
      //   // onDelete: 'NO ACTION',
      //   // onUpdate: 'CASCADE'
      // })
      // Contract.hasMany(models.Problems,{
      //   foreignKey:'problem_id',
      //   // onDelete: 'NO ACTION',
      //   // onUpdate: 'CASCADE'
      // })
    }
  }
  Contract.init(
    {
      number: DataTypes.STRING,
      name: DataTypes.STRING,
      date_start: {
        type: DataTypes.DATEONLY,
        defaultValue: Sequelize.NOW,
      },
      date_end: {
        type: DataTypes.DATEONLY,
        defaultValue: Sequelize.NOW,
      },
      money: DataTypes.FLOAT(2),
      content: DataTypes.TEXT,
      appendix: DataTypes.STRING,
      filename: DataTypes.STRING,
      products: DataTypes.STRING,
      type: DataTypes.STRING,
      approval_status: {
        type: DataTypes.STRING,
        defaultValue: "待审核",
      },
      status: DataTypes.STRING,
      check_sug: DataTypes.STRING,
      // check_staffs:DataTypes.TEXT
    },
    {
      sequelize,
      modelName: "Contract",
      tableName: "contracts",
    }
  );
  return Contract;
};
